package org.rabbitmq.mq02.demo;

import org.rabbitmq.mq02.common.MqException;
import org.rabbitmq.mq02.mqClient.Channel;
import org.rabbitmq.mq02.mqClient.Connection;
import org.rabbitmq.mq02.mqClient.ConnectionFactory;
import org.rabbitmq.mq02.mqServer.core.BasicProperties;
import org.rabbitmq.mq02.mqServer.core.ExchangeType;

import java.io.IOException;

/**
 * 模拟消费者
 */
public class consumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        System.out.println("消费者启动");
        factory.setHost("127.0.0.1");
        factory.setPort(9090);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("queue",true,false,null);

        //接收消息
        boolean ok = channel.basicConsume("queue", true, new org.rabbitmq.mq02.common.Consumer() {
            @Override
            public void handlerDeliver(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                System.out.println("处理消息开始");
                System.out.println("consumerTag:"+consumerTag);
                System.out.println("basicProperties:"+basicProperties);
                System.out.println("body:"+body.toString());
                System.out.println("处理消息结束");
            }
        });
        System.out.println("消费一条消息成功 ok:"+ok);
        // 由于消费者也不知道生产者要生产多少, 就在这里通过这个循环模拟一直等待消费.
        while (true) {
            Thread.sleep(500);
        }
    }
}
